package pack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class MemoryCollections {
	
	public final static int count = 10000;
	public static Runtime runtime = Runtime.getRuntime();
	public static int i;
	public static long before;
	public static long after;
	
	public static void main(String[] args) {
		
		System.gc();
		before = runtime.totalMemory() - runtime.freeMemory();
		int[] array = new int[count];
		
		for(i = 0 ; i < count; i++) {
			array[i] = i;
		}
		System.gc();
		after = runtime.totalMemory() - runtime.freeMemory();
		System.out.println("Array: " + (double)(after-before)/count);
		
		System.gc();
		before = runtime.totalMemory() - runtime.freeMemory();
		Map<Integer, Integer> hash = new HashMap<Integer, Integer>();

		for(i = 0 ; i < count; i++) {
			hash.put(i, i);	
		}
		
		System.gc();
		after = runtime.totalMemory() - runtime.freeMemory();
		System.out.println("HashMap: " + (double)(after-before)/count);
		
		System.gc();
		before = runtime.totalMemory() - runtime.freeMemory();
		IntHashMap inthash = new IntHashMap();

		for(i = 0 ; i < count; i++) {
			inthash.put((int)Math.pow(i, 2), i);	
		}
		
		System.gc();
		after = runtime.totalMemory() - runtime.freeMemory();
		System.err.println("IntHashMap: " + (double)(after-before)/count);
		
		System.gc();
		before = runtime.totalMemory() - runtime.freeMemory();
		Map<Integer, Integer> tree = new TreeMap<Integer, Integer>();

		for(i = 0 ; i < count; i++) {
			tree.put(i, i);
		}
		System.gc();
		after = runtime.totalMemory() - runtime.freeMemory();
		System.out.println("TreeMap: " + (double)(after-before)/count);
		
		System.gc();
		before = runtime.totalMemory() - runtime.freeMemory();
		List<Integer> arrayList = new ArrayList<Integer>();
		for(i = 0 ; i < count; i++) {
			arrayList.add(i);	
		}
		System.gc();
		after = runtime.totalMemory() - runtime.freeMemory();
		System.out.println("ArrayList: " + (double)(after-before)/count);

		System.gc();
		before = runtime.totalMemory() - runtime.freeMemory();
		List<Integer> linkedList = new LinkedList<Integer>();
		for(i = 0 ; i < count; i++) {
			linkedList.add(i);	
		}
		System.gc();
		after = runtime.totalMemory() - runtime.freeMemory();
		System.out.println("LinkedList: " + (double)(after-before)/count);
	}
}
